package cn.xiaobai.domain;

import cn.hutool.json.JSONUtil;
import cn.xiaobai.utils.ConfigReadUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 键适配
 *
 * @Author yangdaji
 * @Date 2022/10/13 0013
 * @PackageName cn.xiaobai.domain
 * @ClassName KeyAdapterModel
 */
@Slf4j
@Data
public class KeyAdapterModel {

    @Value("${key.configName:}")
    private String keyConfigName;

    /**
     * 常见键 映射 , key : 键名称 value: keycode
     */
    private Map<String, Integer> keyAdapterMap = new ConcurrentHashMap<>(8);


    /**
     * 初始化键盘映射
     */
    @PostConstruct
    public void init() {
        if (!StringUtils.isEmpty(keyConfigName)) {
            String keyJson = ConfigReadUtils.readConfigFile(keyConfigName);
            // key : 键名称, value : keycode
            keyAdapterMap = JSONUtil.toBean(keyJson, Map.class);
            log.info("按键初始化完毕!");
        }
    }

    public Map<String, Integer> getKeyAdapterMap() {
        return keyAdapterMap;
    }
}
